# typed: false
# frozen_string_literal: true

class Cangjie < Formula
  desc "SDK for Cangjie Programming Language"
  homepage "https://cangjie-lang.cn/"
  version "1.0.4"
  license "Apache-2.0"

  on_macos do
    on_arm do
      url "https://cangjie-lang.cn/v1/files/auth/downLoad?nsId=142267&fileName=cangjie-sdk-mac-aarch64-1.0.4.tar.gz&objectKey=6911551ca1061e7e7ef142df"
      sha256 "e147f8beb36b98ac049a0c95ae923589d57ec01cbd3c8b7eb58c0721caf10b8f"
    end
  end

  def install
    # Locate the extracted cangjie directory
    cangjie_dir = find_cangjie_directory

    # Install all contents to prefix (bin, lib, tools, etc.)
    cangjie_dir.children.each { |f| prefix.install f }

    # Create symlinks for executables in /opt/homebrew/bin
    link_bin_executables
    link_tools_executables
  end

  # Skip automatic dylib ID fixing - these are pre-built binaries
  # that cannot be modified without recompiling. The libraries use @rpath
  # which works correctly at runtime.
  skip_clean :all

  def post_install
    # Override to skip dylib ID fixing for pre-built binaries
    # Homebrew tries to fix dylib IDs but these binaries cannot be modified
    # without recompiling. The @rpath mechanism works fine at runtime.
  end

  test do
    system "#{bin}/cjc", "--version"
  end

  private

  def find_cangjie_directory
    # Homebrew extracts tar.gz and may move the cangjie directory
    # Check multiple possible locations
    if (buildpath/"cangjie").directory?
      buildpath/"cangjie"
    elsif (buildpath/"bin").directory? && (buildpath/"lib").directory?
      # buildpath itself is the cangjie directory
      buildpath
    elsif (buildpath.parent/"cangjie").directory?
      buildpath.parent/"cangjie"
    else
      odie "cangjie directory not found. buildpath=#{buildpath}, " \
           "exists=#{buildpath.exist?}, directory=#{buildpath.directory?}, " \
           "children=#{buildpath.children.map(&:to_s).join(', ')}"
    end
  end

  def link_bin_executables
    # Link main executables (cjc, cjc-frontend) from prefix/bin to /opt/homebrew/bin
    bin_dir = prefix/"bin"
    return unless bin_dir.directory?

    bin_dir.children.each do |f|
      # Skip symlinks that point to tools/bin (handled separately)
      next if f.symlink? && f.readlink.to_s.include?("tools/bin")

      create_symlink(f, bin)
    end
  end

  def link_tools_executables
    # Link tool executables (cjpm, cjlint, cjfmt, etc.) from prefix/tools/bin
    tools_bin_dir = prefix/"tools/bin"
    return unless tools_bin_dir.directory?

    tools_bin_dir.children.each do |f|
      next unless f.file? || f.symlink?
      create_symlink(f, bin)
    end
  end

  def create_symlink(source, target_dir)
    # Create symlink: /opt/homebrew/bin/executable -> ../../Cellar/cangjie/1.0.4/path/to/executable
    target_dir.mkpath
    target = target_dir/source.basename
    return if target.exist?

    # Calculate relative path from /opt/homebrew/bin to source
    source_rel = source.relative_path_from(target_dir.parent)
    target.make_symlink(source_rel)
  end
end
